package top.healthylife.gateway.platform.softwind.api.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 描述：
 *
 * @author
 * @createtime 2020/2/25 13:46
 */
@Slf4j
public class AESUtils {


    public static final String KEY_ALGORITHM = "AES";
    public static final String ENCODING = "utf-8";
    public static final String KEY = "SYZXjz5otdNNyig/uKXt+w==";



    /**
     * @Author:
     * @Description: 使用默认key
     * @Date: Create in 4:22 下午 2020/9/14
     * @Modified By:
     */
    public static String encrypt(String content) {
        return encrypt(content, KEY);
    }

    public static String encrypt(String content, String key) {
        try {
            log.info("AES加密前字符{}", content);
            byte[] bytesKey = Base64.decodeBase64(key);
            SecretKeySpec secretKey = new SecretKeySpec(bytesKey, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);// 创建密码器
            byte[] byteContent = content.getBytes(ENCODING);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);// 初始化
            byte[] result = cipher.doFinal(byteContent);// 加密
            String res = Base64.encodeBase64String(result);
            log.info("AES加密后字符{}", res);
            return res;
        } catch (Exception e) {
            log.error("encrypt error", e);
        }
        return null;
    }

    /**
     * @Author:
     * @Description: 使用默认key
     * @Date: Create in 4:22 下午 2020/9/14
     * @Modified By:
     */
    public static String decrypt(String content) {
        return decrypt(content, KEY);
    }


    /**
     * base64 decrypt
     * @param content
     * @return
     */
    public static String decryptBase64(String content) {
        try {
            return decrypt(new String(Base64.decodeBase64(content.getBytes()), AESUtils.ENCODING));
        } catch (Exception e) {
            log.error("decryptBase64 解密串：{}， 错误：{}",content,e);
        }
        return null;
    }


    /**
     * base64 encode
     * @param content
     * @return
     */
    public static String encodeBase64(JSONObject content) {
        String encryptResult = encrypt(content.toJSONString());
        try {
            return new String(Base64.encodeBase64(encryptResult.getBytes()), ENCODING);
        } catch (Exception e) {
            log.error("encodeBase64 加密串信息:{},:{}",content,e);
        }
        return null;
    }



    public static String decrypt(String content, String key) {
        try {
            log.info("AES解密前字符{}", content);
            byte[] bytesKey = Base64.decodeBase64(key);
            SecretKeySpec secretKey = new SecretKeySpec(bytesKey, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, secretKey);// 初始化
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));// 解密
            String res = new String(result);
            log.info("AES解密后字符{}", res);
            return res;
        } catch (Exception e) {
            log.error("decrypt error", e);
        }
        return null;
    }


}
